package com.example.sss.service.SharedFileService;

import com.example.sss.controller.VO.SharedFileVO;
import com.example.sss.dao.ActorMapper;
import com.example.sss.dao.SharedFileMapper;
import com.example.sss.model.domin.Actor;
import com.example.sss.model.domin.ObsFile;
import com.example.sss.model.domin.SharedFile;
import com.example.sss.model.domin.User;
import com.example.sss.model.utils.ObsPage;
import com.example.sss.service.ActorService.ActorService;
import com.example.sss.service.ObjectService.BucketObjectService;
import com.example.sss.service.UserService.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class SharedFileServiceImpl implements SharedFileService {
    /**
     * 依赖注入
     */
    @Autowired
    SharedFileMapper sharedFileMapper;
    @Autowired
    private BucketObjectService bucketObjectService;

    @Override
    public ObsPage selectSharedFile(SharedFile sharedFile) {
        PageHelper.startPage(sharedFile.getCurPage(),sharedFile.getPageSize());
        Page<SharedFile> sharedFiles =(Page<SharedFile>) sharedFileMapper.selectSharedFileLists(sharedFile);
        return new ObsPage(sharedFiles.getTotal(),sharedFiles.getResult());
    }

    @Override
    public ObsPage selectSharedFilesList(SharedFile sharedFile) {
        if (sharedFile.getSharedfilePath()==null){
            sharedFile.setSharedfilePath("");
        }
        if (sharedFile.getSharedfilePath().isEmpty()||sharedFile.getSharedfilePath().endsWith("/")) {
            List<ObsFile> files =sharedFileMapper.selectSharedFilesLists(sharedFile);
            //判断是否是根据id搜索
            if (sharedFile.getUserId()==null){
                for(int i=0;i<files.size();i++){
                    int count=count(files.get(i).getPath(), sharedFile.getSharedfilePath().length());
                    //把path移除掉deleteFile
                    if(files.get(i).getPath().endsWith("/")&&count==0){
                        files.remove(files.get(i));
                        i--;
                    }else{
                        //子文件夹下面的也移除掉
                        if (count!=0){
                            files.remove(files.get(i));
                            i--;
                        }
                    }
                }
            }
            int fromIndex =(sharedFile.getCurPage()-1)*sharedFile.getPageSize();
            int toIndex =((sharedFile.getCurPage())*sharedFile.getPageSize()>files.size()?files.size():(sharedFile.getCurPage())*sharedFile.getPageSize()) ;

            List<ObsFile> subList = files.subList(fromIndex,toIndex);
            return new ObsPage(files.size(),subList);
        }else {
            return null;
        }
    }
    /**
     * description:统计从index开始的 '/' 字符数量
     * @param str 被统计字符串
     * @param index 开始位置
     * @return '/' 的数量
     */
    private int count(String str,int index){
        int count=0;
        for(int i=index;i<str.length();i++){
            if (str.charAt(i)=='/'){
                ++count;
            }
        }
        return count;
    }
    @Override
    public SharedFileVO selectSharedFileUser(SharedFile sharedFile) {
        //共享用户组信息
        List<SharedFile> sharedFile1= sharedFileMapper.selectSharedFileLists(sharedFile);
        //用户
        List<User> userList = sharedFileMapper.selectSharedFileUsers(sharedFile.getId());
        SharedFileVO sharedFileVO = new SharedFileVO();
        sharedFileVO.setSharedFile(sharedFile1.get(0));
        sharedFileVO.setUserList(userList);
        return sharedFileVO;
    }

    @Override
    public int addSharedFile(SharedFile sharedFile, List<User> userList) throws IOException {
        sharedFile.setUserId(sharedFile.getCreatePeople());
        //判断是否有重名
        List<SharedFile> sharedFileList = sharedFileMapper.selectSharedFileLists(sharedFile);
        if (sharedFileList.size()>0){
            return 2;
        } else {
            //数据库添加信息
            sharedFile.setCreateTime(new Date());
            //共享的路径要设置成唯一的
            Date currentTime = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            //将日期对象格式化成指定格式并以String输出
            String date = simpleDateFormat.format(currentTime);
            sharedFile.setSharedfilePath(date+sharedFile.getCreatePeople());
            //添加共享文件组
            int i = sharedFileMapper.addSharedFile(sharedFile);
            //添加用户
            for (User user:userList) {
                SharedFile sharedFile1 = new SharedFile();
                sharedFile1.setId(sharedFile.getId());
                sharedFile1.setUserId(user.getId());
                i=sharedFileMapper.addSharedFiles(sharedFile1);
            }
            //在obs共享桶中新建文件夹
            bucketObjectService.newFolder(000,sharedFile.getSharedfilePath());
            return i;
        }
    }

    @Override
    public int updateSharedFile(SharedFile sharedFile) {
        return sharedFileMapper.updateSharedFile(sharedFile);
    }

    @Override
    public int updateShared(SharedFileVO sharedFileVO) {
        SharedFile sharedFile = sharedFileVO.getSharedFile();
        //修改共享文件组
        int i = sharedFileMapper.updateSharedFile(sharedFile);
        //删除用户
        sharedFileMapper.deleteUersByShared(sharedFileVO.getSharedFile().getId());
        //新增用户
        for (User user:sharedFileVO.getUserList()) {
            SharedFile sharedFile1 = new SharedFile();
            sharedFile1.setId(sharedFileVO.getSharedFile().getId());
            sharedFile1.setUserId(user.getId());
            i=sharedFileMapper.addSharedFiles(sharedFile1);
        }
        return i;
    }

    @Override
    public int addSharedFiles(Integer sharedFileId, Integer fileId) {
        SharedFile sharedFile = new SharedFile();
        sharedFile.setId(sharedFileId);
        sharedFile.setUserId(fileId);
        return sharedFileMapper.addSharedFileAndFile(sharedFile);
    }

    @Override
    public int deleteSharedFile(Integer sharedFileId, Integer fileId) {
        SharedFile sharedFile = new SharedFile();
        sharedFile.setId(sharedFileId);
        sharedFile.setFileId(fileId);
        return sharedFileMapper.deleteSharedFiles(sharedFile);
    }
}
